import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/Manager.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Manager',
    component: () => import('../views/Manager.vue'),
    redirect: '/home', // 重定向到主页
    children: [
      { path: 'home', name: 'Home', component: () => import('../views/manager/Home.vue')},
      { path: 'user', name: 'User', component: () => import('../views/manager/User.vue')},
      {path: '/403', name: 'Auth', component: () => import('../views/manager/Auth.vue')},
      {path: 'match', name: 'Match', component: () => import('../views/manager/Match.vue')},
      {path: 'standard', name: 'Standard', component: () => import('../views/manager/Standard.vue')},
      {path: 'plant', name: 'Plant', component: () => import('../views/manager/Plant.vue')},
      {path: 'trace', name: 'Trace', component: () => import('../views/manager/Trace.vue')},
    ]
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('../views/AboutView.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/Register',
    name: 'Register',
    component: () => import('../views/Register.vue')
  },
  {
    path: '/*',
    name: '404',
    component: () => import('../views/404.vue')
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) =>{
  // to 是到达的路由信息
  // from 开通的路由信息
  // next 是帮助跳转路由的函数
  let adminPaths =['/user']
  let user = JSON.parse(localStorage.getItem('wine-user') || '{}')

  if (user.role !== '管理员' && adminPaths.includes(to.path)){
    // 如果当前登录的用户不是管理员，然后当前到达的路径是管理员才有权限访问的路径，那职工时候让用户去到一个没有权限的页面，不让其访问
    next('/403')
  } else {
    next()
  }
})
export default router
